package com.mydemo.utils;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFTable;

import java.io.FileInputStream;
import java.io.IOException;

public class WordRead {

    public static void readDocxFile(String filePath) {
        try (FileInputStream fis = new FileInputStream(filePath);
             XWPFDocument document = new XWPFDocument(fis)) {

            // 读取段落内容
            for (XWPFParagraph paragraph : document.getParagraphs()) {
                System.out.println(paragraph.getText());
            }

            // 读取表格内容（如果有）
            for (XWPFTable table : document.getTables()) {
                // 处理表格数据
                table.getRows().forEach(row -> {
                    row.getTableCells().forEach(cell -> {
                        System.out.print(cell.getText() + "\t");
                    });
                    System.out.println();
                });
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public static void readDocFile(String filePath) {
        try (FileInputStream fis = new FileInputStream(filePath);
             HWPFDocument document = new HWPFDocument(fis)) {

            // 方法一：使用WordExtractor快速提取文本
            WordExtractor extractor = new WordExtractor(document);
            String[] paragraphs = extractor.getParagraphText();
            for (String paragraph : paragraphs) {
                System.out.println(paragraph);
            }

            // 方法二：逐段落读取
            Range range = document.getRange();
            for (int i = 0; i < range.numParagraphs(); i++) {
                Paragraph paragraph = range.getParagraph(i);
                System.out.println(paragraph.text());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static String readWordFile(String filePath) throws IOException {
        StringBuilder content = new StringBuilder();

        if (filePath.toLowerCase().endsWith(".docx")) {
            // 处理 DOCX 文件
            try (FileInputStream fis = new FileInputStream(filePath);
                 XWPFDocument document = new XWPFDocument(fis)) {

                for (XWPFParagraph paragraph : document.getParagraphs()) {
                    content.append(paragraph.getText()).append("\n");
                }
            }
        } else if (filePath.toLowerCase().endsWith(".doc")) {
            // 处理 DOC 文件
            try (FileInputStream fis = new FileInputStream(filePath);
                 HWPFDocument document = new HWPFDocument(fis)) {

                WordExtractor extractor = new WordExtractor(document);
                String[] paragraphs = extractor.getParagraphText();
                for (String paragraph : paragraphs) {
                    content.append(paragraph).append("\n");
                }
            }
        } else {
            throw new IllegalArgumentException("不支持的文件格式");
        }

        return content.toString();
    }

    public static void main(String[] args) {
        String filePath = "C:\\Users\\Administrator\\Desktop\\文件\\中华人民共和国民法典.docx";
        try {
            String content = readWordFile(filePath);
            System.out.println(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
